Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Latest commit

 

History

History
25 lines (22 loc) · 788 Bytes

2.1.3 - Server->on.md

File metadata and controls

25 lines (22 loc) · 788 Bytes

Server->on

注册Server的事件回调函数。

bool Server->on(string $event, mixed $callback);
  • 第1个参数是回调的名称, 大小写不敏感,具体内容参考回调函数列表,事件名称字符串不要加on
  • 第2个函数是回调的PHP函数,可以是函数名的字符串,类静态方法,对象方法数组,匿名函数。

重复调用on方法时会覆盖上一次的设定

$serv = new Swoole\Server("127.0.0.1", 9501);
$serv->on('connect', function ($serv, $fd){
    echo "Client:Connect.\n";
});
$serv->on('receive', function ($serv, $fd, $reactor_id, $data) {
    $serv->send($fd, 'Swoole: '.$data);
    $serv->close($fd);
});
$serv->on('close', function ($serv, $fd) {
    echo "Client: Close.\n";
});
$serv->start();